package com.qf.controller;

import com.qf.service.ProfileService;
import com.qf.tools.JavaWebToken;
import com.qf.tools.ResponseUtils;
import com.qf.tools.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Description TODO
 * @Author 刘珈玮
 * @Date 2020/7/25 19:25
 */
@RestController
@RequestMapping("/profile")
public class Profile {
    @Autowired
    private ProfileService profileService;

    /**
     * 上传头像接口
     * @param profile 文件
     * @param token token验证
     * @return 返回一个ResultEntity类型的成功或者失败信息
     */
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public ResultEntity setUserProfile(@RequestParam(required = true) MultipartFile profile,@RequestParam("token") String token) {
//        Integer userId = user.getUserId();
        Map<String, Object> stringObjectMap = JavaWebToken.parserJavaWebToken(token);
        if (stringObjectMap != null) {
            Integer userId = (Integer) stringObjectMap.get("userId");
            boolean b = profileService.updUserProfile(profile, userId);
            if (b) {
                return ResponseUtils.responseClinet(true);
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }
}
